Prepare Data (copied from JABaron_leaflet.Rmd)

Load Baltimore vacant (i.e. boarded up) houses data

balt_vacant <- read.socrata("https://data.baltimorecity.gov/resource/rw5h-nvv4.csv")

Tidy location data

location <- str_split(balt_vacant$location, pattern = " ", simplify = TRUE) %>%
    as_tibble()
colnames(location) <- c("location_type", "longitude", "latitude")

location <- purrr::map_dfc(location,
                      ~ str_remove_all(.x, pattern = "\\(|\\)") %>%
                          str_trim(.)
                      ) %>%
            type_convert(.)

balt_vacant <- cbind(balt_vacant, location)

lon_range <- range(balt_vacant$longitude)
lat_range <- range(balt_vacant$latitude)

Load Baltimore neighborhood boundaries

To give someone not familiar with Baltimore some context

neighborhoods <- read_sf("https://data.baltimorecity.gov/resource/h3fx-54q3.geojson")
nb <- st_read("https://data.baltimorecity.gov/resource/h3fx-54q3.geojson")
## Reading layer `h3fx-54q3' from data source `https://data.baltimorecity.gov/resource/h3fx-54q3.geojson' using driver `GeoJSON'
## Simple feature collection with 278 features and 6 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -76.71141 ymin: 39.19723 xmax: -76.52967 ymax: 39.372
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
nb <- neighborhoods$geometry[[1]] %>%
    as.matrix() %>%
    as_tibble() %>%
    rename(longitude = V1, latitude = V2)

Try out plotly

plot_ly() %>%
    add_trace(data = balt_vacant, type = 'scattergeo', mode = 'markers',
                lon = ~longitude, lat = ~latitude
                ) %>%
    add_trace(data = neighborhoods, type = 'scattergeo', mode = 'lines') %>%
    layout(geo = list(scope = 'USA'))
plot_geo() %>%
    add_trace(data = balt_vacant, type = 'scattergeo', mode = 'markers',
                lon = ~longitude, lat = ~latitude
                ) %>%
        # this doesn't draw neighborhood lines for some reason (object type?)
    add_polygons(data = nb, x = ~longitude, y = ~latitude) %>%
    layout(geo = list(
        scope = 'usa',
        lonaxis = list(range = lon_range),
        lataxis = list(range = lat_range)
        )
        )
plot_ly(neighborhoods)
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
class(neighborhoods)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"
class(nb)
## [1] "tbl_df"     "tbl"        "data.frame"
View(nb)
as.matrix(neighborhoods$geometry)
## Geometry set for 278 features 
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -76.71141 ymin: 39.19723 xmax: -76.52967 ymax: 39.372
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## First 5 geometries:
## MULTIPOLYGON (((-76.61113 39.32344, -76.61167 3...
## MULTIPOLYGON (((-76.67263 39.29184, -76.67262 3...
## MULTIPOLYGON (((-76.56852 39.33594, -76.56814 3...
## MULTIPOLYGON (((-76.68626 39.3479, -76.68646 39...
## MULTIPOLYGON (((-76.5588 39.30646, -76.55892 39...
t <- neighborhoods$geometry[[1]]
str(t)
## List of 1
##  $ :List of 1
##   ..$ : num [1:61, 1:2] -76.6 -76.6 -76.6 -76.6 -76.6 ...
##  - attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')

# geo styling
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  subunitcolor = toRGB("gray85"),
  countrycolor = toRGB("gray85"),
  countrywidth = 0.5,
  subunitwidth = 0.5
)

p <- plot_geo(df, lat = ~lat, lon = ~long) %>%
  add_markers(
    text = ~paste(airport, city, state, paste("Arrivals:", cnt), sep = "<br />"),
    color = ~cnt, symbol = I("square"), size = I(8), hoverinfo = "text"
  ) %>%
  colorbar(title = "Incoming flights<br />February 2011") %>%
  layout(
    title = 'Most trafficked US airports<br />(Hover for airport)', geo = g
  )
p
head(df)
##   iata                           airport              city state country
## 1  ORD      Chicago O'Hare International           Chicago    IL     USA
## 2  ATL William B Hartsfield-Atlanta Intl           Atlanta    GA     USA
## 3  DFW   Dallas-Fort Worth International Dallas-Fort Worth    TX     USA
## 4  PHX  Phoenix Sky Harbor International           Phoenix    AZ     USA
## 5  DEN                       Denver Intl            Denver    CO     USA
## 6  IAH      George Bush Intercontinental           Houston    TX     USA
##        lat       long   cnt
## 1 41.97960  -87.90446 25129
## 2 33.64044  -84.42694 21925
## 3 32.89595  -97.03720 20662
## 4 33.43417 -112.00806 17290
## 5 39.85841 -104.66700 13781
## 6 29.98047  -95.33972 13223
#### DON'T WORK!!! ####
# from https://moderndata.plot.ly/visualizing-geo-spatial-data-with-sf-and-plotly/
plot_ly(franconia, split = ~NAME_ASCI)

plot_ly(
  franconia, 
  split = ~NUTS_ID,
  color = ~SHAPE_AREA,
  alpha = 1,
  showlegend = FALSE
)
# from https://moderndata.plot.ly/visualizing-geo-spatial-data-with-sf-and-plotly/
# not evaluated
plot_ly(franconia, split = ~NAME_ASCI, type = 'scattergeo', mode = 'lines',
        linetype = 'solid')
%>%
    layout(geo = list(
        scope = 'usa',
        lonaxis = list(range = lon_range),
        lataxis = list(range = lat_range)
        )
        )

plot_ly(
  franconia, 
  split = ~NUTS_ID,
  color = ~SHAPE_AREA,
  alpha = 1,
  showlegend = FALSE
)

CONCLUSION

I don’t really like plotly for maps. Maybe combining it with something else would make it better but it isn’t very intuitive and needs a lot of tweaking to make it work.